package com.hison.tandainbao.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.hison.tandainbao.entiy.pojo.domain.TbLocalculture;
import com.hison.tandainbao.entiy.pojo.domain.User;
import com.hison.tandainbao.entiy.pojo.dto.LocalcultueDto;
import com.hison.tandainbao.mapper.TbLocalcultureMapper;
import com.hison.tandainbao.mapper.UserMapper;
import com.hison.tandainbao.response.ResponseResult;
import com.hison.tandainbao.service.TbLocalcultureService;
import com.hison.tandainbao.util.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author hison
 * @description 针对表【tb_localculture】的数据库操作Service实现
 * @createDate 2024-03-22 21:36:42
 */
@Service
public class TbLocalcultureServiceImpl extends ServiceImpl<TbLocalcultureMapper, TbLocalculture>
        implements TbLocalcultureService {


    @Autowired
    private TbLocalcultureMapper tbLocalcultureMapper;


    /**
     * 添加本地文化信息
     * *
     *
     * @param localcultueDto localcultue dto
     * @return @return {@code ResponseResult }
     * @author $USER$
     * @date 2024/03/22
     * @see LocalcultueDto
     * @see ResponseResult
     * @since version
     */
    @Override
    @Transactional
    public ResponseResult add(LocalcultueDto localcultueDto) {

        //获取当前登录用户的id
        Long id = UserHolder.getUser().getId();
        //将信息进行重新封装
        localcultueDto.setUserid(id);
        //设置文化信息发布状态为待审核状态
        localcultueDto.setStatus(1);
        //保存上传的图片信息（由前端封装）
        //将文化信息表 ，同时保存到状态审核表
        //1.先将信息存入信息表
        int insert = tbLocalcultureMapper.insert(localcultueDto);
        if (insert < 1) {
            return ResponseResult.error("添加失败");
        }
        return ResponseResult.ok("添加成功，等待管理员审核");
    }

    /**
     * 用户 获取当地文化 只能获取到审核成功的
     *
     * @return @return {@code ResponseResult }
     * @author $USER$
     * @date 2024/03/29
     * @see ResponseResult
     * @since version
     */
    @Override
    public ResponseResult getLocalCulture() {
        List<TbLocalculture> tbLocalcultures = query().eq("status", 2).orderByAsc("create_time").list();
        return ResponseResult.ok(tbLocalcultures);
    }

    /**
     * 删除 用户自己发布的博客
     *
     * @param cultureId 区域性 ID
     * @return @return {@code ResponseResult }
     * @author $USER$
     * @date 2024/03/29
     * @see Integer
     * @see ResponseResult
     * @since version
     */
    @Override
    public ResponseResult delete(Integer cultureId) {
        Long id = UserHolder.getUser().getId();
        QueryWrapper<TbLocalculture> wrapper = new QueryWrapper<>();
        wrapper.eq("culture_id", cultureId);
        wrapper.eq("userId", id);
        int delete = tbLocalcultureMapper.delete(wrapper);
        if (delete < 1) {
            return ResponseResult.error("删除失败");
        }
        return ResponseResult.ok("删除成功");
    }

    /**
     * 保存草稿文化
     *
     * @param tbLocalculture 结核病本地文化
     * @return @return {@code ResponseResult }
     * @author $USER$
     * @date 2024/03/29
     * @see TbLocalculture
     * @see ResponseResult
     * @since version
     */
    @Override
    public ResponseResult saveDraftCulture(TbLocalculture tbLocalculture) {
        tbLocalculture.setUserid(UserHolder.getUser().getId());
        tbLocalculture.setStatus(0);
        tbLocalculture.setCreateTime(DateTime.now());
        tbLocalculture.setUpdateTime(DateTime.now());
        int i = tbLocalcultureMapper.insert(tbLocalculture);
        if (i < 1) {
            return ResponseResult.error("保存失败");
        }
        return ResponseResult.ok("保存成功");
    }

    @Override
    public ResponseResult getMyCulture() {
        QueryWrapper<TbLocalculture> wrapper = new QueryWrapper<>();
//        wrapper.eq("userId", UserHolder.getUser().getId());
        wrapper.eq("userId", 1L);
        List<TbLocalculture> list = tbLocalcultureMapper.selectList(wrapper);
        if (list.size() == 0) {
            return ResponseResult.ok("查询结果为空");
        }
        return ResponseResult.ok("查询成功", list);
    }

    /**
     * 获取我保存草稿
     *
     * @return @return {@code ResponseResult }
     * @author $USER$
     * @date 2024/03/29
     * @see ResponseResult
     * @since version
     */
    @Override
    public ResponseResult getMySaveDraftCulture() {
        QueryWrapper<TbLocalculture> wrapper = new QueryWrapper<>();
        wrapper.eq("userId", UserHolder.getUser().getId());
        wrapper.eq("status", 0);
        List<TbLocalculture> list = tbLocalcultureMapper.selectList(wrapper);
        if (list.size() == 0) {
            return ResponseResult.ok("查询结果为空");
        }
        return ResponseResult.ok("查询成功", list);
    }

    /**
     * 将用户的草稿，改为发布状态
     *
     * @param cultureId 区域性 ID
     * @return @return {@code ResponseResult }
     * @author $USER$
     * @date 2024/03/29
     * @see Integer
     * @see ResponseResult
     * @since version
     */
    @Override
    public ResponseResult putCulture(Integer cultureId) {
        Long id = UserHolder.getUser().getId();
        int i = tbLocalcultureMapper.updateInfo(cultureId, id);
        if (i < 1) {
            return ResponseResult.error("发布失败");
        }
        return ResponseResult.ok("发布成功");
    }

    /**
     * 修改文化信息
     *
     * @return @return {@code ResponseResult }
     * @author $USER$
     * @date 2024/03/29
     * @see String
     * @see ResponseResult
     * @since version
     */
    @Override
    public ResponseResult updateCultureInfo(TbLocalculture tbLocalculture) {
        boolean b = updateById(tbLocalculture);
        if (!b) {
            return ResponseResult.error("更新失败");

        }
        return ResponseResult.ok("更新成功");
    }



    @Override
    public ResponseResult getCultureInfoByUserName(String userName) {
        List<TbLocalculture> list = tbLocalcultureMapper.getCultureInfoByUserName(userName);
        if (list == null) {
            return ResponseResult.error("该用户还未发布信息");
        }
        return ResponseResult.ok(list);
    }


    //   ---------------------------- 管理员方法 ------------------------------------
    //审核发布的文化信息
    @Override
    public ResponseResult checkLocalCulture(int cultureId, String status) {
        int i = tbLocalcultureMapper.checkLocalCuture(cultureId, status);
        if (i < 1) {
            return ResponseResult.error("更新失败");
        }
        return ResponseResult.ok("更新成功");
    }


    //查看所有的文化信息
    @Override
    public ResponseResult getAllCultures(Integer current,  Integer total) {
        PageHelper.startPage(current, total);
        List<TbLocalculture> cultures = query().orderByAsc("status","create_time").list();
        //获取发布人
        cultures.forEach(item -> {
            Long userid = item.getUserid();
            item.setUserName(getUserName(userid));
        });
        return ResponseResult.ok("查询成功", cultures);
    }

    @Autowired
    private UserMapper  userMapper;
    private String getUserName(Long userid) {
        User user = userMapper.selectById(userid);

        return  user == null ?"":user.getName();
    }

    @Override
    public ResponseResult deleteCulturesByAdmin(Integer culturesId) {
        int i = tbLocalcultureMapper.deleteById(culturesId);
        if (i < 1) {
            return ResponseResult.error("删除失败");
        }
        return ResponseResult.ok("删除成功");
    }

}
